<?php
session_start();
error_reporting('0');
header('Content-Type: text/html; charset=utf-8');

///
set_include_path(dirname(dirname(__FILE__)) . '/lib/');
require_once 'OpenSDK/Tencent/Weibo.php';

///
$list = array(2, 3);
$app = intval($_GET['app']);
if ( ! in_array($app, $list)) 
	$app = '';
	
include "appkey{$app}.php";

///
OpenSDK_Tencent_Weibo::init($appkey, $appsecret);

///
if (isset($_GET['go_oauth'])) {
	/// 跳转
	goAuth();
	
} elseif (isset($_GET['exit'])) {
	/// 退出
	unset($_SESSION[OpenSDK_Tencent_Weibo::OAUTH_TOKEN]);
	unset($_SESSION[OpenSDK_Tencent_Weibo::ACCESS_TOKEN]);
	unset($_SESSION[OpenSDK_Tencent_Weibo::OAUTH_TOKEN_SECRET]);
}
if(isset($_SESSION[OpenSDK_Tencent_Weibo::ACCESS_TOKEN]) && isset($_SESSION[OpenSDK_Tencent_Weibo::OAUTH_TOKEN_SECRET])) {
	/// 已登录应用
	getInfo();
	
} elseif (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
	/// 取得回调信息
	getInfo();
	
}

echo '<p><a href="?go_oauth&app='.$app.'">点击去授权</a></p>';
echo '<p><a href="?exit">退出</a></p>';

/**
 * 
 */
function goAuth()
{
	global $app;
	
	$callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?app=' . $app;
	$request_token = OpenSDK_Tencent_Weibo::getRequestToken($callback);
	$url = OpenSDK_Tencent_Weibo::getAuthorizeURL($request_token);
	header('Location: ' . $url);
}

/**
 * 
 */
function getInfo()
{
	if (OpenSDK_Tencent_Weibo::getAccessToken($_GET['oauth_verifier'])) {
		$uinfo = OpenSDK_Tencent_Weibo::call('user/info');
		pr($_SESSION);
		pr($uinfo);
	} else {
		echo "失败";
	}
}

/**
 * 调试
 */
function pr($var)
{
	echo '<pre>';
	var_export($var);
	echo '</pre>';
}
